global cropflip_asm

section .rodata

section .text
;void tiles_asm(
;		unsigned char *src,
;		unsigned char *dst,
;		int cols, rows
;              	int src_row_size,
;              	int dst_row_size,
;		int tamx, int tamy,
;		int offsetx, int offsety
;		);

cropflip_asm:
	;rdi(*src)
	;rsi(*dst)
	;edx(cols)
	;ecx(fils)
	;r8d(src_row_size)
	;r9d(dst_row_size)
	push rbp
	mov rbp, rsp
	push rbx
	push r12
	push r13
	push r14
	push r15
	sub rsp, 8
	
	mov rax, rbp
	add rax, 16
	mov r12d, dword [rax]  ;tamx
	add rax, 8
	mov r13d, dword [rax]  ;tamy
	add rax, 8
	mov r14d, dword [rax]  ;offsetx
	add rax, 8
	mov r15d, dword [rax]  ;offsety
	
	mov edx, edx
	mov ecx, ecx
	mov r8d, r8d
	mov r9d, r9d
	mov r12d, r12d
	mov r13d, r13d
	mov r14d, r14d
	mov r15d, r15d

	%define i r10
	%define j r11
	%define cols rdx
	%define fils rcx
	%define tamx r12
	%define tamy r13
	%define offsetx r14
	%define offsety r15
	
	mov rax, r15
	mul r8
	add rdi, rax
	shl r14, 2
	add rdi, r14			;me paro al principio del cuadrado de la imagen fuente
	mov rax, r13
	mul r9
	add rsi, rax
	shl r12, 2
	sub rsi, r12			 ;me paro al principio de la ultima fila del destino
	shr r12, 2

	xor i,i
	xor j,j
	.Filas:
	cmp i, tamy
	je .Fin
	xor j, j
	mov r14, rdi			;preservo los punteros a los principios de las filas
	mov r15, rsi			;fuente y destino (para pasar facil de fila)
	.Cols:
	cmp j, tamx 
	je .FinCols
	movdqu xmm0, [rdi]
	
	;add rax, rbx
	;add rax, rbx
	;mov rax, [rsp]
	;mov [rsp], rax

	movdqu [rsi], xmm0
	add rdi, 16
	add rsi, 16
	add j, 4
	jmp .Cols
	.FinCols:
	mov rdi, r14
	add rdi, r8
	mov rsi, r15
	sub rsi, r9
	inc i
	jmp .Filas
	.Fin:
	add rsp, 8
	pop r15
	pop r14
	pop r13
	pop r12
	pop rbx
	pop rbp
    	ret
